#include "svpwm.h"
#include <math.h>

// 整个逻辑，先根据alphabeta判断扇区，并计算出角度，根据扇区确定基准矢量
// 并计算出相应的时间Tx和Ty,需要一个角度

void svpwm_set_abc(alphabeta_voltage_handle alphabeta_ptr)
{
    float angle_for_calculate;
    float alpha = alphabeta_ptr->ualpha;
    float beta = alphabeta_ptr->ubeta;
    float angle = atan2f(beta,alpha)*(180.0/M_PI);
    float ref = sqrtf(alpha*alpha+beta*beta);
    
    float bool_a = beta;
    float bool_b = sqrtf(3)/2*alpha-0.5f*beta;
    float bool_c = -sqrtf(3)/2*alpha-0.5f*beta;

    float m = sqrtf(3)*ref/u_ref;

    uint8_t a,b,c;

    if(bool_a>0){a=1;}else{a=0;}
    if(bool_b>0){b=1;}else{b=0;}
    if(bool_c>0){c=1;}else{c=0;}

    uint16_t T_1,T_2,T_0;
    uint16_t CCR_A,CCR_B,CCR_C;

    sector = a+2*b+4*c;

    switch (sector) {
        case SECTOR_1:
            // 计算矢量计算所需要的角度值，需要更改一下，统一到一个轴上
            angle_for_calculate = angle*M_PI / 180.0f;
            T_1 = m*T*sinf((60-angle_for_calculate)*M_PI / 180.0f);
            T_2 = m*T*sinf(angle_for_calculate*M_PI / 180.0f);
            T_0 = (T-T_1-T_2)/2;
            CCR_A = T_0;
            CCR_B = T_0 + T_1;
            CCR_C = T_0 + T_1 + T_2;
            break;
        
        case SECTOR_2:
            angle_for_calculate = (angle-60.0)*M_PI / 180.0f;
            T_1 = m*T*sinf((60-angle_for_calculate)*M_PI / 180.0f);
            T_2 = m*T*sinf(angle_for_calculate*M_PI / 180.0f);
            T_0 = (T-T_1-T_2)/2;
            CCR_B = T_0;
            CCR_A = T_0 + T_2;
            CCR_C = T_0 +T_1 + T_2;
            break;

        case SECTOR_3:
            angle_for_calculate = (angle-120.0)* M_PI / 180.0f;
            T_1 = m*T*sinf((60-angle_for_calculate)*M_PI / 180.0f);
            T_2 = m*T*sinf(angle_for_calculate*M_PI / 180.0f);
            T_0 = (T-T_1-T_2)/2;
            CCR_B = T_0;
            CCR_C = T_0 + T_1;
            CCR_A = T_0 +T_1 + T_2;

            break;

        case SECTOR_4:
            angle_for_calculate = (angle+180.0)* M_PI / 180.0f;
            T_1 = m*T*sinf((60-angle_for_calculate)*M_PI / 180.0f);
            T_2 = m*T*sinf(angle_for_calculate*M_PI / 180.0f);
            T_0 = (T-T_1-T_2)/2;
            CCR_C = T_0;
            CCR_B = T_0 + T_2;
            CCR_A = T_0 +T_1 + T_2;
            break;

        case SECTOR_5:
            angle_for_calculate = (angle+120.0)* M_PI / 180.0f;
            T_1 = m*T*sinf((60-angle_for_calculate)*M_PI / 180.0f);
            T_2 = m*T*sinf(angle_for_calculate*M_PI / 180.0f);
            T_0 = (T-T_1-T_2)/2;
            CCR_C = T_0;
            CCR_A = T_0 + T_1;
            CCR_B = T_0 +T_1 + T_2;
            break;

        case SECTOR_6:
            angle = 0;
            angle_for_calculate = (angle+60)* M_PI / 180.0f;
            T_1 = m*T*sinf((60-angle_for_calculate)*M_PI / 180.0f);
            T_2 = m*T*sinf(angle_for_calculate*M_PI / 180.0f);
            T_0 = (T-T_1-T_2)/2;
            CCR_A = T_0;
            CCR_C = T_0 + T_2;
            CCR_B = T_0 +T_1 + T_2;
            break;
        default:
            break;
    }

    // 中心对齐式的pwm波形，up-down模式输出pwm
    EPWM_setCounterCompareValue(epwm1_BASE,EPWM_COUNTER_COMPARE_A,CCR_A);
    EPWM_setCounterCompareValue(epwm1_BASE,EPWM_COUNTER_COMPARE_B,CCR_A);
    EPWM_setCounterCompareValue(epwm2_BASE,EPWM_COUNTER_COMPARE_A,CCR_B);
    EPWM_setCounterCompareValue(epwm2_BASE,EPWM_COUNTER_COMPARE_B,CCR_B);
    EPWM_setCounterCompareValue(epwm3_BASE,EPWM_COUNTER_COMPARE_A,CCR_C);
    EPWM_setCounterCompareValue(epwm3_BASE,EPWM_COUNTER_COMPARE_B,CCR_C);
}